package behavioralPatterns.command;

interface Command {
    void execute();
}

class TurnOnCommand implements Command {
    private Light light;

    public TurnOnCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.turnOn();
    }
}

class TurnOffCommand implements Command {
    private Light light;

    public TurnOffCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.turnOff();
    }
}

class Light {
    public void turnOn() {
        System.out.println("Light is on");
    }

    public void turnOff() {
        System.out.println("Light is off");
    }
}

class RemoteControl {
    private Command[] commands;

    public RemoteControl() {
        commands = new Command[2];
    }

    public void setCommand(Command command, int slot) {
        commands[slot] = command;
    }

    public void pressButton(int slot) {
        commands[slot].execute();
    }
}

public class CommandPattern {
    public static void main(String[] args) {
        Light light = new Light();
        TurnOnCommand turnOnCommand = new TurnOnCommand(light);
        TurnOffCommand turnOffCommand = new TurnOffCommand(light);
        RemoteControl remoteControl = new RemoteControl();
        remoteControl.setCommand(turnOnCommand, 0);
        remoteControl.setCommand(turnOffCommand, 1);
        remoteControl.pressButton(0);
        remoteControl.pressButton(1);
    }
}
